package com.chuanyang.driverh.mainmy.fragment;

import android.app.Activity;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.chuanyang.driverh.R;
import com.chuanyang.driverh.base.BaseFragment;
import com.chuanyang.driverh.base.OnCustomListener;
import com.chuanyang.driverh.bean.IdentifyCarBean;
import com.chuanyang.driverh.mainmy.AddLinesActivity;
import com.chuanyang.driverh.mainmy.CarIdentifyActivity;
import com.chuanyang.driverh.mainmy.adapter.LinesManageAdapter;
import com.chuanyang.driverh.responseresult.LinesManageData;
import com.chuanyang.driverh.responseresult.LinesManageResult;
import com.chuanyang.driverh.utils.ConstUtil;
import com.chuanyang.driverh.utils.DialogUtil;
import com.chuanyang.driverh.utils.PrintUtil;
import com.chuanyang.driverh.utils.SharedPrefUtil;
import com.chuanyang.driverh.utils.eventbus.ReUpdateLinesEvent;
import com.chuanyang.driverh.utils.net.JsonGenericsSerializator;
import com.chuanyang.driverh.utils.net.MyCallback;
import com.chuanyang.driverh.utils.net.MyHttpUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import cn.qqtheme.framework.picker.OptionPicker;
import okhttp3.Call;

public class IdentifyCarFragment extends BaseFragment {
    private TextView tv_identify_addline;
    private ListView lv_identify_addline;
    private TextView tv_next;
    private EditText et_car_cartype;
    private EditText et_car_carcode;
    private EditText et_car_carstandard;
    private EditText et_car_maxpower;
    private EditText et_car_minpower;
    private EditText et_car_driverinsurance;

    private LinearLayout ll_car_color;
    private TextView tv_car_color;
    private View popView;
    private ListView lv_colors;
    private String[] mcolors;
    private ViewPager pager;

    private LinesManageAdapter myAdapter;
    private ArrayList<LinesManageData.DataBean> linesList;
    private String carcolor = "";

    private IdentifyCarBean identifyCarBean;
    private boolean ifCarPerson;//如果为true表示是个人认证加车辆认证   false表示为车辆认证
    @Override
    protected int getLayoutResId() {
        return R.layout.my_frag_identifycar;
    }
    @Override
    protected void initView(View view) {
        tv_next = (TextView) getActivity().findViewById(R.id.tv_caridentify_next);
        tv_identify_addline = (TextView) rootView.findViewById(R.id.tv_identify_addline);//添加常用路线
        lv_identify_addline = (ListView) rootView.findViewById(R.id.lv_identify_addline);
        //车辆类型
        et_car_cartype = (EditText) rootView.findViewById(R.id.et_car_cartype);
        //车牌号
        et_car_carcode = (EditText) rootView.findViewById(R.id.et_car_carcode);
        //车辆规格
        et_car_carstandard = (EditText) rootView.findViewById(R.id.et_car_carstandard);
        //司机运力
        et_car_maxpower = (EditText) rootView.findViewById(R.id.et_car_maxpower);
        //实际运力
        et_car_minpower = (EditText) rootView.findViewById(R.id.et_car_minpower);
        //车辆保险单
        et_car_driverinsurance = (EditText) rootView.findViewById(R.id.et_car_driverinsurance);
        //车牌颜色
        ll_car_color = (LinearLayout) rootView.findViewById(R.id.ll_car_color);
        tv_car_color = (TextView) rootView.findViewById(R.id.tv_car_color);

    }

    @Override
    protected void initData() {
        mcolors = new String[]{"蓝色","黄色","黑色","白色","其它"};
        getLines();
        if (getActivity() instanceof CarIdentifyActivity) {
            identifyCarBean = ((CarIdentifyActivity) getActivity()).getIdentifyCarBean();
            ifCarPerson = false;
            pager = ((CarIdentifyActivity) getActivity()).getPager();

        } else {
//            ifCarPerson = true;
//            identifyCarBean = ((CarPersonIdentifyActivity) getActivity()).getIdentifyCarBean();
//            pager = ((CarPersonIdentifyActivity) getActivity()).getPager();


        }
    }
    private void setEntity() {
        identifyCarBean.setCarType(et_car_cartype.getText().toString().trim());//车型
        identifyCarBean.setPlateNumber(et_car_carcode.getText().toString().trim());
        identifyCarBean.setCarStandard(et_car_carstandard.getText().toString().trim());
        identifyCarBean.setActualCapacity(et_car_minpower.getText().toString().trim());
        identifyCarBean.setCapacity(et_car_maxpower.getText().toString().trim());
        identifyCarBean.setDriverInsurance(et_car_driverinsurance.getText().toString().trim());//保险单

        // addedCarInfoSub.setCarMark(et_car_carcode.getText().toString().trim());
//        addedCarInfoSub.setCompanyID("1");
//        addedCarInfoSub.setGsmNumber(et_car_sim.getText().toString().trim());
//        addedCarInfoSub.setTerminalID(et_car_sb.getText().toString().trim());
//        addedCarInfoSub.setTerminalVer(et_car_type.getText().toString().trim());
        identifyCarBean.setCarMarkColor(carcolor);
        //identifyCarBean.setAddedCarInfoSub(addedCarInfoSub);

    }

    @Override
    protected void initListener() {
        ll_car_color.setOnClickListener(this);
        tv_next.setOnClickListener(this);
        tv_identify_addline.setOnClickListener(this);
        lv_identify_addline.setAdapter(getAdapter());
        myAdapter.setOnCustomListener(new OnCustomListener() {
            @Override
            public void onCustomerListener(View v, int position) {
                switch (v.getId()) {
                    case R.id.iv_lines_delete:
                        deleteLines(myAdapter.getItem(position));
                        break;
                }
            }
        });
    }

    private void deleteLines(final LinesManageData.DataBean bean) {
        DialogUtil.showLoadingDialog(getActivity(),"删除路线...");
        Map<String,String> params=new HashMap<String, String>();
        params.put("id", bean.getId() + "");
        MyHttpUtil.deleteLines(getActivity(),params, new MyCallback<LinesManageResult>(getActivity(),new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                PrintUtil.toast(getActivity(), "网络异常，请稍后再试");
            }
            @Override
            public void onSuccess(LinesManageResult result, int id) {
                if (result.getCode() == ConstUtil.OK) {
                    myAdapter.remove(bean);
                    lv_identify_addline.requestLayout();
                    PrintUtil.toast(getActivity(),"路线删除成功");
                }else{
                    PrintUtil.toast(getActivity(),result.getMsg());
                }

            }
        });


    }
    private void getLines() {
        DialogUtil.showLoadingDialog(getActivity(),"路线...");
        Map<String,String> params=new HashMap<String, String>();
        params.put("driverId", SharedPrefUtil.getUserId());
        MyHttpUtil.getLines(getActivity(),params, new MyCallback<LinesManageResult>(getActivity(),new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                PrintUtil.toast(getActivity(), "网络异常，请稍后再试");
            }
            @Override
            public void onSuccess(LinesManageResult result, int id) {
                if (result.getCode() == ConstUtil.OK) {
                    myAdapter.clear();
                    myAdapter.addAll(result.getData().getData());
                }else{
                    PrintUtil.toast(getActivity(),result.getMsg());
                }
            }
        });



    }

    public LinesManageAdapter getAdapter() {
        if (linesList == null) {
            linesList = new ArrayList();
        }
        if (myAdapter == null) {
            myAdapter = new LinesManageAdapter(getActivity(), linesList);
        }
        return myAdapter;
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.tv_caridentify_next:
                if (checkall()) {
                    setEntity();
                    pager.setCurrentItem(1);
                }
                break;
            case R.id.tv_identify_addline:
                startActivity(AddLinesActivity.class);
                break;
            case R.id.ll_car_color:
                onOptionPicker(getActivity(),tv_car_color,mcolors);
                break;
        }
    }
    private boolean checkall() {
        if (TextUtils.isEmpty(et_car_cartype.getText().toString())) {
            PrintUtil.toast(getActivity(),"请填写车辆类型");
            return false;
        } else if (TextUtils.isEmpty(et_car_carcode.getText().toString())) {
            PrintUtil.toast(getActivity(),"请填写车牌号");
            return false;
        } else if (TextUtils.isEmpty(et_car_carstandard.getText().toString())) {
            PrintUtil.toast(getActivity(),"请填写车辆规格");
            return false;
        } else if (TextUtils.isEmpty(et_car_minpower.getText().toString().trim())) {
            PrintUtil.toast(getActivity(),"请填写车辆最小载重");
            return false;
        } else if (TextUtils.isEmpty(et_car_maxpower.getText().toString().trim())) {
            PrintUtil.toast(getActivity(),"请填写车辆运力");
            return false;
        } else if (Integer.parseInt(et_car_minpower.getText().toString().trim()) > Integer.parseInt(et_car_maxpower.getText().toString().trim())) {
            PrintUtil.toast(getActivity(),"车辆最小载重不能大于车辆运力");
            return false;

        } else if (TextUtils.isEmpty(et_car_driverinsurance.getText().toString())) {
            PrintUtil.toast(getActivity(),"请填写车辆保险单");
            return false;
        }
        else if (TextUtils.isEmpty(tv_car_color.getText().toString())) {
            PrintUtil.toast(getActivity(),"选择车牌颜色");
            return false;
        } else if (myAdapter.getCount() <= 0) {
            PrintUtil.toast(getActivity(),"请至少添加一条路线");
            return false;
        }
        return true;
    }

    public void onEventMainThread(ReUpdateLinesEvent event) {
        if (event.isUpdate()) {
            getLines();
        }

    }


    public  void onOptionPicker(Activity context, final TextView view, String[] strs ){
        OptionPicker picker = new OptionPicker(context, strs);
        picker.setCycleDisable(false);
        picker.setLineVisible(true);
        picker.setShadowVisible(false);
        picker.setTopPadding(6);
        picker.setTextSize(14);
        picker.setOnOptionPickListener(new OptionPicker.OnOptionPickListener() {
            @Override
            public void onOptionPicked(int index, String item) {
                view.setText(item);
                carcolor=(index+1)+"";
            }
        });
        picker.show();
    }


}


